package cn.labbook.bluetooth.util.validate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MobileUtils {

    /**
     * 中国手机号码
     */
    private static Pattern CHINESE_PHONE_PATTERN = Pattern.compile("((13|15|16|17|18|19)\\d{9})|(14[57]\\d{8})");


    /**
     * 是否是有效的中国手机号码
     * @param phone
     * @return
     */
    public static boolean isValidChinesePhone(String phone) {
        if (phone == null || phone.length() != 11) {
            return false;
        }

        Matcher matcher = CHINESE_PHONE_PATTERN.matcher(phone);
        return matcher.matches();
    }

    /**
     * 检查手机是否无效
     * @param phone
     * @return
     */
    public static boolean isNotValidChinesePhone(String phone) {
        return !isValidChinesePhone(phone);
    }

    /**
     * 隐藏中间字符
     *
     * @param mobile    手机号
     * @return          处理后的手机号
     */
    public static String hiddenCenter(String mobile) {
        if (isNotValidChinesePhone(mobile)) {
            return mobile;
        }
        return mobile.substring(0, 3) + "****" + mobile.substring(mobile.length() - 4, mobile.length());
    }

    /**
     * 第二位字符开始隐藏
     *
     * @param mobile    手机号
     * @return          处理后的手机号
     */
    public static String hiddenSecond(String mobile) {
        if (isNotValidChinesePhone(mobile)) {
            return mobile;
        }
        return mobile.substring(0, 1) + "******" + mobile.substring(mobile.length() - 4, mobile.length());
    }

}
